package com.adamfass.jmeutil;

import java.util.HashMap;

import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.MaterialState;
import com.jme.system.DisplaySystem;

/**
 * Static helpers for displaying debug info. If not initialized, methods do nothing.
 */
public class DebugUtil {
  private DebugUtil() {};
  
  private static boolean initialized = false;
  private static final HashMap<String, Node> nameToDebugMarker = 
      new HashMap<String, Node>();
  private static Node markerRootNode = null;
  
  public static void initialize(Node markerRootNodeParam) {
    initialized = true;
    markerRootNode = markerRootNodeParam;
  }
  
  private static Node createDebugMarker(String name, ColorRGBA color) {
    Node markerNode = new Node("debug marker " + name);
    
    final Sphere sphere = new Sphere("debug marker sphere " + name, 10, 10, 15);
    final MaterialState materialState = 
      DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
    materialState.setEnabled(true);
    materialState.setDiffuse(color);
    materialState.setAmbient(color);
    sphere.setRenderState(materialState);
    markerNode.attachChild(sphere);
    
    return markerNode;
  }
  
  public static void showDebugMarker(String name, ColorRGBA color, Vector3f position) {
    if (!initialized) {
      return;
    }
    
    Node marker = nameToDebugMarker.get(name);
    if (marker == null) {
      marker = createDebugMarker(name, color);
      nameToDebugMarker.put(name, marker);
      markerRootNode.attachChild(marker);
      marker.updateRenderState();
    }
    marker.setLocalTranslation(position.x, position.y, position.z);
    marker.updateGeometricState(0, true);
  }
}
